<template>
  <div center>
    <el-form size="mini" ref="postForm" :rules="rules" :inline="true" :model="postForm" :label-position="'right'"
             label-width="80px">
      <el-row type="flex" justify="space-between" :gutter="20">
        <el-form-item/>
        <el-form-item prop="shr" v-if="!setAuditUser">
          <el-button type="warning" size="mini" @click="setAudit">
            提交审核
          </el-button>
        </el-form-item>
        <el-form-item v-if="!setFabuUser">
          <el-button type="warning" size="mini" @click="setAudit">
            发布
          </el-button>
        </el-form-item>
        <el-form-item v-if="setAuditInfo">
          <el-button type="primary" size="mini" @click="auditState('EXPENSE')">
            审核通过
          </el-button>
          <el-button type="danger" size="mini" @click="auditState('INVALID')">
            审核失败
          </el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="现场调查记录表">
        <table1 ref="table1" :no-created="true" :isShow="isShow"/>
      </el-tab-pane>
      <el-tab-pane label="现场检测方案" v-if="info.tableNode>=1">
        <table2 ref="table2" :no-created="true" :isShow="isShow"/>
      </el-tab-pane>
      <el-tab-pane label="工作日写实记录表" v-if="info.tableNode>=2">
        <table3 ref="table3" :no-created="true" :isShow="isShow"/>
      </el-tab-pane>
      <el-tab-pane label="采样记录表" v-if="info.tableNode>=3">
        <table4List ref="table4List" :no-created="true" :isShow="isShow"/>
      </el-tab-pane>
      <el-tab-pane label="样品交接表" v-if="info.tableNode>=6">
        <table5 ref="table5" :no-created="true" :isShow="isShow2"/>
      </el-tab-pane>
      <el-tab-pane label="实验室原始记录" v-if="info.tableNode>=7">
        <table6List ref="table6List" :no-created="true" :isShow="isShow2"/>
      </el-tab-pane>
      <el-tab-pane label="分析记录表" v-if="info.tableNode>=8">
        <table7List ref="table7List" :no-created="true" :isShow="isShow2"/>
      </el-tab-pane>
      <el-tab-pane label="检测评价报告" v-if="info.tableNode>=10">
        <table9 ref="table9" :no-created="true" :isShow="isShow3"/>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import table1 from '@/views/table/table1'
import table2 from '@/views/table/table2'
import table3 from '@/views/table/table3'
import table5 from '@/views/table/table5'
import table4List from '@/views/table/table4List'
import table6List from '@/views/table/table6List'
import table7List from '@/views/table/table7List'
import table9 from '@/views/table/table9'
import { addOrEdit, getInfo, setAuditUser, auditState, del } from '@/api/table/tableOne'
import fileUpdate from '@/components/Upload/fileUpdate'
import { getRoleId, getUserId } from '@/utils/auth'
import { getUserList } from '@/api/system/sysUser'

export default {
  components: {
    table1, table2, table3, table5, table4List, table6List, table7List, table9
  },
  props: {
    noCreated: {
      type: Boolean,
      default:
        null
    }
  },
  data() {
    return {
      activeName: '0',
      info: {},
      // 查询条件
      postForm: {},
      // 查询条件
      selectForm: {},
      userList: [],
      userId: getUserId(),
      roleId: getRoleId(),
      setAuditUser: false,
      setAuditInfo: false, // 审核通过 审核失败
      setFabuUser: false,
      isShow: false,
      isShow2: false,
      isShow3: false,
      rules: {
        auditUser: [{ required: true, message: '请选择内容', trigger: 'chang' }]
      }
    }
  }, created() {
    this.getUserList()
  },
  methods: {
    handleClick(tab, event) {
      if (tab.paneName == 0) {
        this.$refs.table1.init(this.info.id)
      } else if (tab.paneName == 1) {
        this.$refs.table2.init(this.info.id)
      } else if (tab.paneName == 2) {
        this.$refs.table3.init(this.info.id)
      } else if (tab.paneName == 3) {
        this.$refs.table4List.init(this.info.id, this.info.userId)
      } else if (tab.paneName == 4) {
        this.$refs.table5.init(this.info.id)
      } else if (tab.paneName == 5) {
        this.$refs.table6List.init(this.info.id)
      } else if (tab.paneName == 6) {
        this.$refs.table7List.init(this.info.id)
      } else if (tab.paneName == 7) {
        this.$refs.table9.init(this.info.id)
      }
    },
    getInfo(id) {
      getInfo(id).then(response => {
        console.log(getRoleId())
        // 审核通过 审核失败
        if (response.data.auditUser != null) {
          if ((response.data.tableNode == 5 || response.data.tableNode == 9 || response.data.tableNode == 11 || response.data.tableNode == 13) && (response.data.auditUser.indexOf(getUserId()) != -1 || getRoleId().indexOf('1116226986067390465') != -1)) {
            this.setAuditInfo = true
          }
        }
        this.info = response.data
        // 提交审核 为false的时候就展示
        if (this.info.userId != this.userId || this.info.auditState == 'WAIT' || (this.info.tableNode != 4 && this.info.tableNode != 8 && response.data.tableNode != 10)) {
          this.setAuditUser = true
        } else {
          this.setAuditUser = false
        }
        // 发布
        if ((this.info.userId != this.userId && getRoleId().indexOf('1116226986067390465') < 0) || (this.info.tableNode != 12)) {
          this.setFabuUser = true
        }

        if (this.info.userId != this.userId || this.info.tableNode > 4) {
          this.isShow = true
        }
        if (this.info.userId != this.userId || this.info.tableNode > 8) {
          this.isShow2 = true
        }
        if (this.info.userId != this.userId || this.info.tableNode > 10) {
          this.isShow3 = true
        }
      })
    },
    // 获取所有用户
    getUserList() {
      getUserList().then(response => {
        this.userList = response.data
      })
    },
    auditState(state) {
      this.$confirm('是否确定该操作', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        auditState(this.info.id, state).then(response => {

          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 2000
          })
          this.$parent.$parent.getData()
          this.$parent.$parent.centerDialogVisible = false
        })
      })
    },
    //提交审核
    setAudit() {
      setAuditUser(this.info.id).then(response => {
        this.$notify({
          title: '成功',
          message: '已提交等待审核',
          type: 'success',
          duration: 2000
        })
        this.$parent.$parent.getData()
        this.$parent.$parent.centerDialogVisible = false
      })
    },
    // 初始化
    init(id) {
      this.activeName = '0'
      this.setAuditUser = false
      this.setAuditInfo = false
      this.isShow = false
      this.isShow2 = false
      if (id != null) {
        this.getInfo(id)
        this.$refs.table1.init(id)
      } else {
        this.info = {}
        this.setFabuUser = true
        this.setAuditUser = true
        this.$refs.table1.init(null)
      }
    }
  }
}
</script>

